package com.beiing.tianshuai.tianshuai.message.ui;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.app.AppConstant;
import com.beiing.tianshuai.tianshuai.base.BaseActivity;
import com.netease.nim.uikit.common.util.string.StringUtil;

import butterknife.BindView;
import butterknife.OnClick;

public class EditMyGroupNickNameActivity extends BaseActivity {

    /**
     * 群昵称最大长度
     */
    private static final int MAX_LENGTH = 32;

    @BindView(R.id.toolbar_iv_back)
    ImageView mToolbarIvBack;
    @BindView(R.id.toolbar_tv_title)
    TextView mToolbarTvTitle;
    @BindView(R.id.toolbar_btn_publish)
    TextView mToolbarBtnPublish;
    @BindView(R.id.et_my_new_group_nick_name)
    EditText mEtMyNewGroupNickName;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_edit_my_group_nick_name;
    }

    @Override
    protected void init() {
        initToolbar();
        initListener();
    }

    /**
     * 初始化监听事件
     */
    private void initListener() {
        mEtMyNewGroupNickName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                int editEnd = mEtMyNewGroupNickName.getSelectionEnd();
                mEtMyNewGroupNickName.removeTextChangedListener(this);
                while (StringUtil.counterChars(s.toString()) > MAX_LENGTH && editEnd > 0) {
                    s.delete(editEnd - 1, editEnd);
                    editEnd--;
                }
                mEtMyNewGroupNickName.setSelection(editEnd);
                mEtMyNewGroupNickName.addTextChangedListener(this);
            }
        });
    }

    private void initToolbar() {
        mToolbarTvTitle.setText("修改群昵称");
        mToolbarIvBack.setVisibility(View.VISIBLE);
        mToolbarBtnPublish.setVisibility(View.VISIBLE);
        mToolbarBtnPublish.setText("完成");
    }

    @OnClick({R.id.toolbar_iv_back, R.id.toolbar_btn_publish})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.toolbar_iv_back:
                onBackPressed();
                break;
            case R.id.toolbar_btn_publish:
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(mEtMyNewGroupNickName.getApplicationWindowToken(), 0);
                }
                String result = mEtMyNewGroupNickName.getText().toString().trim();
                if (!TextUtils.isEmpty(result)) {
                    Intent intent = new Intent();
                    intent.putExtra("data", result);
                    setResult(AppConstant.EDIT_MY_GROUP_NICK_NAME, intent);
                    finish();
                } else {
                    showShortText("群昵称不能为空");
                }
                break;
        }
    }
}
